<?php
/**
 * 用户管理
 */
namespace Admin\Controller;
use Admin\Controller\AdminController;
class MemberController extends AdminController {
    public function _initialize(){
        //权限验证
        if(!authCheck(MODULE_NAME."/".CONTROLLER_NAME."/".ACTION_NAME,session('userid'))){
            $this->error('你没有权限!');
        }           
    }    
	//用户列表
    public function index(){
        $this->assign('title','用户管理');
        $result="";
        $gid = I('gid');
        if(!empty($gid)){
            //当前组的所有用户
            $result = D('MemberView')->getGroupUsers(10,$gid,'id asc'); 
        }else{
            //系统所有用户
            $result = D('MemberView')->getUsers(10,'id asc');
        }
        $this->group = M('auth_group')->where('status=1')->select();
        $this->assign('users',$result['users']);
        $this->assign('page',$result['page']);
    	$this->display();
    }
    //添加用户
    public function AddUser(){
        $user = D('Member');
        if(!$user->create()){
            $this->error($user->getError());
        }else{
            $data['username']=I('username');
            $data['email']=I('email');
            $data['score']=I('score');
            $data['password']=I('password','','md5');
            $data['regtime'] = time();
            $data['logintime'] = time();
            $data['loginip'] = get_client_ip();
            if($uid=$user->add($data)){
                if(M('auth_group_access')->add(array('uid'=>$uid,'group_id'=>I('group_id')))){
                    $this->success("添加成功");
                }else{
                    $this->error("添加失败");
                }
            }
        }
        
    }
    //编辑用户
    public function editUser(){
        $id = I("get.id");
        if(empty($id)) $this->error("参数错误!");
        $this->user = D("MemberView")->where("member.id=".$id)->select();
        $this->group = M('auth_group')->select();
        $this->assign('title','编辑用户');
        $this->display();
    }
    //编辑用户
    public function editUserHandle(){    
        $member = M('member');
        $group = M('auth_group_access');
        $rules = array(
            array('username','require','修改失败:用户名不能为空！'),
            array('username','','修改失败:用户名已经存在！',0,'unique',1), 
            array('group_id','require','修改失败:请选择用户组'),
            array('email','require','修改失败:邮箱不能为空！'),
            array('email','email','email格式错误'),  
            array('email','','修改失败:邮箱已经存在！',0,'unique',1),             
        );
        if (!$member->validate($rules)->create()){
            $this->error($member->getError());
        }else{
            $data['username'] = I('username');
            $data['email'] = I('email');
            $data['score'] = I('score');
            $data['status'] = I('status');
            $data['id']   =I('id');
            $password = I('password');
            $arr['group_id'] = I('group_id');
            if(!empty($password)) $data['password'] = I('password','','md5');
            $result1 = $member->save($data);
            $result2 = $group->where("uid=".I('id'))->save($arr);  
            ($result1 || $result2) ? $this->success("修改成功"):$this->error("修改失败");
        }
    }
    //删除用户
    public function delUser(){
        $this->error("此功能暂时禁止使用!");
        // $id = I("id");
        // $uid = M('member')->where("id=".$id)->delete();
        // M('auth_group_access')->where("uid=".$uid)->delete();
        // if($uid){
        //     $this->success("删除成功");
        // }else{
        //     $this->error("删除失败");
        // }
       
    }

}